// pages/cart/cart.js
Page({
  /**
   * 页面的初始数据
   */
  checkedAll: false,
  total: 0,
  data: {
    goodsInfo: [],
    // 图片地址前置
    baseImg: 'https://dianjiu.oss-cn-beijing.aliyuncs.com',
  },
  // 全选
  checkedAll(e) {
    this.setData({
      checkedAll: !this.data.checkedAll,
    })
    let appInst = getApp()
    let id = e.currentTarget.dataset.id
    appInst.globalData.cartGoods.forEach((i) => {
      i.checked = this.data.checkedAll
    })
    this.setNewGoodsInfo(appInst.globalData.cartGoods)
    wx.setStorageSync('goodsInfo', appInst.globalData.cartGoods)
    this.setTotal()
  },
  // 商品详情跳转
  toDetails(e) {
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/pages/details/details',
      events: {
        acceptDataFromOpenedPage: function (data) {},
      },
      success: function (res) {
        // 通过 eventChannel 向被打开页面传送数据
        res.eventChannel.emit('acceptDataFromOpenerPage', { id })
      },
    })
  },
  // 单个选中
  checkboxChange(e) {
    let appInst = getApp()
    let id = e.currentTarget.dataset.id
    appInst.globalData.cartGoods.forEach((i) => {
      if (i.commodity_id === id) {
        i.checked = !i.checked
      }
    })
    this.setNewGoodsInfo(appInst.globalData.cartGoods)
    wx.setStorageSync('goodsInfo', appInst.globalData.cartGoods)
    this.setTotal()

    let bool = appInst.globalData.cartGoods.some((i) => {
      return i.checked === false
    })
    if (bool) {
      this.setData({
        checkedAll: false,
      })
    } else {
      this.setData({
        checkedAll: true,
      })
    }
  },
  // 删除该商品
  del(e) {
    let appInst = getApp()
    let id = e.currentTarget.dataset.id
    appInst.globalData.cartGoods = appInst.globalData.cartGoods.filter(
      (i) => i.commodity_id !== id
    )
    this.setNewGoodsInfo(appInst.globalData.cartGoods)
    wx.setStorageSync('goodsInfo', appInst.globalData.cartGoods)
    this.setTotal()
  },
  // 设置goodsInfo
  setNewGoodsInfo(val) {
    this.setData({
      goodsInfo: val || [],
    })
  },
  // 改变商品数量
  changeNum(e) {
    let appInst = getApp()
    let id = e.currentTarget.dataset.id
    let symbol = e.currentTarget.dataset.symbol
    appInst.globalData.cartGoods.forEach((i) => {
      if (i.commodity_id === id) {
        if (symbol === '+') i.num++
        else if (symbol === '-') {
          if (i.num == 1) return
          i.num--
        } else {
          console.log(e)
        }
      }
    })
    this.setNewGoodsInfo(appInst.globalData.cartGoods)
    wx.setStorageSync('goodsInfo', appInst.globalData.cartGoods)
    this.setTotal()
  },
  aaa(e) {
    let appInst = getApp()
    let id = e.currentTarget.dataset.id
    let num = e.detail.value
    appInst.globalData.cartGoods.forEach((i) => {
      if (i.commodity_id === id) {
        i.num = num
      }
    })
    this.setNewGoodsInfo(appInst.globalData.cartGoods)
    wx.setStorageSync('goodsInfo', appInst.globalData.cartGoods)
    this.setTotal()
  },
  bbb(e) {},
  // 设置合计
  setTotal() {
    let total = this.data.goodsInfo
      .filter((i) => i.checked)
      .reduce((pre, cur) => (pre += cur.num * cur.commodity_price), 0)
    this.setData({
      total,
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.setNewGoodsInfo(wx.getStorageSync('goodsInfo'))
    this.setTotal()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.setData({
      goodsInfo: wx.getStorageSync('goodsInfo') || [],
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      goodsInfo: wx.getStorageSync('goodsInfo') || [],
    })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {},

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {},
})
